import { ApiResponse, LoginType, Token } from '@/types/data'
import { LoginAction, RootThunkAction } from '@/types/store'
import requset from '@/utils/request'
import { removeToken, setToken } from '@/utils/storage'

export const login = (values: LoginType): RootThunkAction => {
  return async dispatch => {
    const res = await requset.post<ApiResponse<Token>>('/authorizations', values)
    //存到reduce中
    dispatch({
      type: 'login/login',
      payload: res.data.data,
    })
    //存储带本地
    setToken(res.data.data)
  }
}

export const getCode = (mobile: string): RootThunkAction => {
  return async dispatch => {
    const res = await requset.get(`/sms/codes/${mobile}`)
    console.log(res)
  }
}

//退出功能
export const logout = (): LoginAction => {
  //清除本地的token
  removeToken()
  return {
    type: 'login/logout',
  }
}
